package com.minhua.jijiehao2.mainframe.mine.usercenteractivity.impl

import android.content.Intent
import android.os.Handler
import android.view.View
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.constants.DefineKey
import com.minhua.jijiehao2.mainframe.main.impl.MainActivity
import com.minhua.jijiehao2.mainframe.mine.updatenicknameactivity.impl.UpdateNickNameActivity
import com.minhua.jijiehao2.mainframe.mine.updatepasswordactivity.impl.UpdatePasswordActivity
import com.minhua.jijiehao2.mainframe.mine.usercenteractivity.imp.IUserCenterView
import com.minhua.jijiehao2.utils.SPUtils
import com.minhua.jijiehao2.utils.T
import kotlinx.android.synthetic.main.activity_user_center.*

class UserCenterActivity : BaseActivity<IUserCenterView, UserCenterPresenter>(), IUserCenterView {

    override fun getLayoutId(): Int = R.layout.activity_user_center

    override fun getPresenter(): UserCenterPresenter = UserCenterPresenter()

    private val mOnClickListener = View.OnClickListener { v ->
        when (v.id) {
            R.id.tvNickName -> startActivity(Intent(this, UpdateNickNameActivity::class.java))
            R.id.tvUpdatePassword -> startActivity(Intent(this, UpdatePasswordActivity::class.java))
            R.id.tvLoginOut -> mPresenter.loginOut()
        }
    }

    override fun initView() {
        tvNickName.viewHolder.setText(R.id.tvRight, SPUtils.get(DefineKey.USER_NICK_NAME, "") as String)
        tvNickName.setOnClickListener(mOnClickListener)
        tvUpdatePassword.setOnClickListener(mOnClickListener)
        tvLoginOut.setOnClickListener(mOnClickListener)
    }

    override fun onLogout() {
        T.showShort("退出登录成功")
        SPUtils.remove(DefineKey.LOGIN_STATE)
        SPUtils.remove(DefineKey.USER_NAME)
        SPUtils.remove(DefineKey.USER_ID)
        SPUtils.remove(DefineKey.TOKEN)
        Handler().postDelayed({ startActivity(Intent(this, MainActivity::class.java)) }, 500)
    }

}
